package com.toubutu.audio.admin.controller;

import com.toubutu.audio.admin.dto.RoleDto;
import com.toubutu.audio.admin.dto.RolePermissionBindDto;
import com.toubutu.audio.admin.dto.UserRoleBindDto;
import com.toubutu.audio.admin.service.RoleService;
import com.toubutu.audio.admin.vo.RoleVo;
import com.toubutu.audio.common.PermissionCode;
import com.toubutu.common.PageUtils;
import com.toubutu.common.Result;
import com.toubutu.permission.Permission;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 戒燥
 * @since 2023-09-30
 */
@RequestMapping("/admin/role")
@Api(tags = "后台角色管理接口")
@RestController
public class RoleController {

    @Resource
    private RoleService roleService;
    @GetMapping("/list/{page}/{size}")
    @ApiOperation(value = "角色列表")
//    @Permission(PermissionCode.Role.ROLE_LIST)
    public Result<PageUtils<RoleVo>> getRolePageList(@PathVariable(value = "page")Long page,
                                                     @PathVariable(value = "size")Long size
                                                      ){
        PageUtils<RoleVo> rolePageList = roleService.getRolePageList(size, page);
        return Result.ok(rolePageList);
    }

    @GetMapping("/list/all")
    @ApiOperation(value = "获取所有角色")
    public Result<List<RoleVo>> getAllRoleList(){
        return Result.ok(roleService.getAllRoles());
    }

    @DeleteMapping("/delete/{roleId}")
    @ApiOperation("根据角色id删除对应角色")
//    @Permission(PermissionCode.Role.ROLE_DELETE)
    public Result<Boolean> deleteRoleById(@PathVariable("roleId")Integer roleId){
        return Result.ok(roleService.deleteRoleById(roleId));
    }
    @PostMapping("/add")
    @ApiOperation("新增角色")
//    @Permission(PermissionCode.Role.ROLE_ADD)
    public Result<Boolean> saveRole(@RequestBody RoleDto roleDto){
        return Result.ok(roleService.saveRole(roleDto));
    }

    @PostMapping("/update")
    @ApiOperation("修改角色信息")
//    @Permission(PermissionCode.Role.ROLE_UPDATE)
    public Result<Boolean> updateRole(@RequestBody RoleDto roleDto){
        return Result.ok(roleService.updateRole(roleDto));
    }

    @PostMapping("/bindPermission")
    @ApiOperation("为角色分配权限")
//    @Permission(PermissionCode.Permission.PERMISSION_BIND)
    public Result<Boolean> bindRoleToMenu(@RequestBody RolePermissionBindDto roleBind){
        return Result.ok(roleService.bindRoleToPermission(roleBind));
    }

    @PostMapping("/bindUser")
    @ApiOperation("为用户分配角色")
//    @Permission(PermissionCode.Role.ROLE_BIND_USER)
    public Result<Boolean> bindRoleToUser(@RequestBody UserRoleBindDto bindDto){
        return Result.ok(roleService.bindRoleToUser(bindDto));
    }

    @GetMapping("/bindPermissionIds/{roleId}")
    @ApiOperation("获取角色已经绑定的权限id")
    public Result<List<Integer>> getPermissionList(@PathVariable("roleId") Integer roleId){
        return Result.ok(roleService.getBindPermissionIdsByRoleId(roleId));
    }

    @GetMapping("/info/{roleId}")
    @ApiOperation("获取角色信息")
//    @Permission(PermissionCode.Role.ROLE_DETAIL)
    public Result<RoleVo> getRoleInfoById(@PathVariable("roleId") Integer roleId){
        return Result.ok(roleService.getRoleInfoById(roleId));
    }

    @GetMapping("/enable/{roleId}/{isEnable}")
    @ApiOperation("是否启用角色")
    public Result<Void> roleEnable(@PathVariable("roleId")Integer roleId,
                                      @PathVariable("isEnable") Integer isEnable){
        roleService.enableRole(roleId,isEnable);
        return Result.ok();
    }
}
